import type { AnyObject } from "@/types";
import type { IMsgProps } from "@/components/Message/index.vue";
import Message from "@/components/Message/index.vue";
import { createApp } from "vue";

/**
 * url参数拼接
 * @param url 原本url地址
 * @param param 拓展参数
 * @returns
 */
export function translateURL(url: string, param: AnyObject): string {
  let endURL = "";
  Object.entries(param).forEach((item, index) => {
    const [key, value] = item;
    endURL += `${!index ? "?" : "&"}${key}=${value}`;
  });
  return url + endURL;
}

export function showToast(
  options: IMsgProps & { onCancel?: () => void; onConfirm?: () => void }
) {
  const {
    title = "标题",
    content,
    confirmText = "确定",
    cancelText = "取消",
    onCancel = () => {},
    onConfirm = () => {},
  } = options;
  const dom = document.createElement("div");
  document.body.appendChild(dom);
  const app = createApp(Message, {
    show: true,
    title,
    content,
    confirmText,
    cancelText,
    onCancel: async (callBack?: Function) => {
      await onCancel();
      callBack?.().then(() => {
        app.unmount();
        dom.remove();
      });
    },
    onConfirm: async (callBack?: Function) => {
      await onConfirm();
      callBack?.().then(() => {
        app.unmount();
        dom.remove();
      });
    },
  });
  app.mount(dom);
}
